const mongoose = require('../utils/db')

const userSchema = new mongoose.Schema({
    username: String,
    phone: String,
    password: String,
    img: String,
    age: Number,
    sex: String
})
var User = mongoose.model('users', userSchema)

// 插入一个用户
const insertUser = (userinfo) => {
    const user = new User(userinfo)
    return new Promise((resolve, reject) => {
        user.save((err, doc) => {
            if (err) reject(err)
            else resolve(doc)
        })
    })
}

// 查找用户
const isUserExist = (userinfo) => {

    return new Promise((resolve, reject) => {
        User.find(userinfo, (err, docs) => {
            if (err) {
                reject(err)
            } else {
                resolve({ isExist: docs.length > 0, userinfo: docs[0] })
            }
        })
    })

}

module.exports = {
    insertUser,
    isUserExist
}